(function ($) {

    var validifyElemPropsBucket = new Array();
    var valididyTooltipStyle = {
        "background-color": "#FBBABB",
        "border": "1px solid #CF001B",
        "padding": "7px",
        "display": "none",
        "color": "#CF001B",
        "text-align": "left",
        "font-size": "12px",
        "-moz-box-shadow": "0 0 10px #333333",
        "-webkit-box-shadow": "0 0 10px #333333"
    };

    var valididyElementStyle = {
        "background-color": "#FBBABB"
    };

    jQuery.fn.vValidify = function (options) {
        var tooltipStyleMerge = jQuery.extend({}, jQuery.fn.vValidify.defaults.tooltipStyle, options.tooltipStyle);
        options.tooltipStyle = tooltipStyleMerge;
        var opts = jQuery.extend({}, jQuery.fn.vValidify.defaults, options);
        return this.each(function () {
            opts.elementOriginalStyle = $(this).attr("class");
            render($(this), opts);
            addToValidifyElemPropsBucket($(this), opts);
        }); 
    };

    jQuery.fn.vValidify.defaults = {
        regExp: "[]*",
        required: false,
        errorMessage: "Invalid Field.",
        tooltipStyle: valididyTooltipStyle,
        elementErrorStyle: valididyElementStyle,
        elementOriginalStyle: null,
        rangeValue: {
            min: null,
            max: null
        },
        rangeLength: {
            min: null,
            max: null
        },
        match: null
    };

    jQuery.fn.vIsValid = function () {
        var props = validifyElemPropsBucket[$(this).attr("id")];
        var isValid = true;
        if (props.required && isEmpty($(this)) && isValid) {
            isValid = false;
        }
        if (!checkRegExp($(this), props.regExp) && isValid) {
            isValid = false;
        }
        return isValid;
    };

    jQuery.fn.vShowError = function () {
        var props = validifyElemPropsBucket[$(this).attr("id")];
        var temp = $(this).tooltip();
        temp.show();
        $(this).css(props.elementErrorStyle);
        return true;
    };

    jQuery.fn.vHideError = function () {
        var props = validifyElemPropsBucket[$(this).attr("id")];
        var temp = $(this).tooltip();
        temp.hide();
        alert(props.elementOriginalStyle);
        $(this).css(props.elementOriginalStyle);
        return true;
    };

    function render ($obj, opts) {
        
        $obj = createNewTooltipContainer($obj, opts);

        $($obj).attr("title", opts.errorMessage);

        $($obj).tooltip({
            // place tooltip on the right edge
            position: "center right",
            // a little tweaking of the position
            offset: [-2, 10],
            // use the built-in fadeIn/fadeOut effect
            effect: "fade",
            // custom opacity setting
            opacity: 0.7,
            // use this single tooltip element
            tip: "#"+generateTooltipContainerId($obj),
            // use this tooltip element to define the events for the toolpit
            events: {
                def: "null, null",
                input: "null, null", // 'show events, hide events'
                widget: "null, null"
            }
        });

    }

    function createNewTooltipContainer ($obj, opts) {
        var tooltipContainerId = generateTooltipContainerId($obj);
        var html = $('<div>').append($($obj).clone()).remove().html();
        $obj = $($obj).replaceWith(html+"<div id="+tooltipContainerId+"></div>");
        $("#"+tooltipContainerId).css(opts.tooltipStyle);
        return $("#"+$obj.attr("id"));
    }

    function generateTooltipContainerId ($obj) {
        return "v"+$($obj).attr("id")+"TooltipContainer";
    }

    function addToValidifyElemPropsBucket ($obj, opts) {
        var newObj = jQuery.extend({}, opts, {id:$obj.attr("id")});
        validifyElemPropsBucket[$obj.attr("id")] = newObj;
    }

    function isEmpty ($obj) {
        return (($obj.attr("value") == "") || ($obj.attr("value") == null)) ? true : false;
    }

    function checkRegExp ($obj, regExp) {
        var ePattern = new RegExp(regExp);
        return ePattern.test($obj.attr("value"));
    }

    jQuery.vSetTooltipStyle = function (options) {
        valididyToolipStyle = options;
        $("#vTooltipContainer").css(valididyToolipStyle);
        return true;
    };

})(jQuery);

